//
//  LGCheckPhoneController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/14.
//  验证手机号

#import "LGCheckPhoneController.h"
//设置新密码
#import "LGSetNewPasswordController.h"
#import "LGAccountInputView.h"
@interface LGCheckPhoneController ()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)LGAccountInputView *codeView;
/**   */
@property (nonatomic , strong)UIButton *connectBtn;
/**   */
@property (nonatomic , strong)UIButton *nextBtn;
/**   */
@property (nonatomic , strong)NSTimer *timer;
/**   */
@property (nonatomic , assign)NSInteger seconds;
@end

@implementation LGCheckPhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"验证当前手机号";
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.codeView];
    [self.view addSubview:self.connectBtn];
    [self.view addSubview:self.nextBtn];
}

-(void)setPhone:(NSString *)phone{
    _phone = phone;
    NSString *phoneStr = [phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    self.titleLabel.text = [NSString stringWithFormat:@"请验证当前手机号%@",phoneStr];
}

//获取验证码
-(void)getSmsCodeAction:(UIButton *)sender{
    [RequestUtil withPOST:@"/doctor/my.setting/updateMobileSms" parameters:@{@"mobile":self.phone} withSign:NO success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.seconds = 60;
            [self.timer invalidate];
            self.timer = nil;
            self.codeView.actionBtn.selected = YES;
            self.codeView.actionBtn.userInteractionEnabled = NO;
            [self.codeView.actionBtn setTitle:[NSString stringWithFormat:@"重新发送(%ld)",self.seconds] forState:UIControlStateNormal];
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
}

//验证码倒计时
-(void)countDownAction{
    self.seconds--;
    [self.codeView.actionBtn setTitle:[NSString stringWithFormat:@"重新发送(%ld)",self.seconds] forState:UIControlStateNormal];
    if (self.seconds == 0) {
        self.codeView.actionBtn.selected = NO;
        self.codeView.actionBtn.userInteractionEnabled = YES;
        [self.codeView.actionBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
    }
}

//判断手机格式
- (BOOL)checkTelNumber:(NSString *)telNumber{
    NSString *pattern = @"^1+[0123456789]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}

//联系客服
-(void)connectBtnAction{
    
}

//下一步
-(void)nextBtnAction{
    [RequestUtil withPOST:@"/doctor/my.setting/verifyMobile" parameters:@{@"mobile":self.phone,@"code":self.codeView.contenTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            LGSetNewPasswordController *controller = [[LGSetNewPasswordController alloc]init];
            controller.backVC = self.backVC;
            controller.phone = self.phone;
            controller.code = self.codeView.contenTF.text;
            [self.navigationController pushViewController:controller animated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"请验证当前手机号158****4973" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), viewPix(250), viewPix(40));
    }
    return _titleLabel;
}

-(LGAccountInputView *)codeView{
    if(!_codeView){
        _codeView = [[LGAccountInputView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.titleLabel.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(56))];
        [_codeView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _codeView.backgroundColor = [UIColor whiteColor];
        _codeView.type = @"code";
        _codeView.contenTF.attributedPlaceholder = [@"请输入验证码" attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]}];
        _codeView.hasBorder = NO;
        [_codeView.actionBtn addTarget:self action:@selector(getSmsCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeView;
}

-(UIButton *)connectBtn{
    if(!_connectBtn){
        _connectBtn = [UIButton buttonWithTitle:@"手机号无法验证？去联系客服" titleFont:LGFont(14) textColor:@"#999999" imageName:@"grayTip12"];
        _connectBtn.frame = CGRectMake(Screen_W-viewPix(250), kMaxY(self.codeView.frame)+viewPix(10), viewPix(230), viewPix(32));
        _connectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_connectBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:4];
        [_connectBtn addTarget:self action:@selector(connectBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _connectBtn;
}

-(UIButton *)nextBtn{
    if(!_nextBtn){
        _nextBtn = [UIButton buttonWithTitle:@"下一步" topMargin:kMaxY(self.codeView.frame)+viewPix(164)];
        [_nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
